package com.soladhoc.annonces.client.ui;

import com.google.api.gwt.oauth2.client.Auth;
import com.google.api.gwt.oauth2.client.AuthRequest;
import com.google.api.gwt.oauth2.client.Callback;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.user.client.Window;
import com.google.inject.Inject;
import com.gwtplatform.mvp.client.Presenter;
import com.gwtplatform.mvp.client.View;
import com.gwtplatform.mvp.client.annotations.NameToken;
import com.gwtplatform.mvp.client.annotations.ProxyCodeSplit;
import com.gwtplatform.mvp.client.proxy.ProxyPlace;
import com.gwtplatform.mvp.client.proxy.RevealRootContentEvent;
import com.soladhoc.annonces.client.place.NameTokens;

public class OAuthPresenter extends
		Presenter<OAuthPresenter.MyView, OAuthPresenter.MyProxy> {

	// Use the implementation of Auth intended to be used in the GWT client app.
	private static final Auth AUTH = Auth.get();
	

	private static String authToken = null;
	
	public interface MyView extends View {
		HasClickHandlers getAuthButton();
	}

	@ProxyCodeSplit
	@NameToken(NameTokens.oauth)
	public interface MyProxy extends ProxyPlace<OAuthPresenter> {
	}

	@Inject
	public OAuthPresenter(final EventBus eventBus, final MyView view,
			final MyProxy proxy) {
		super(eventBus, view, proxy);
	}

	@Override
	protected void revealInParent() {
		RevealRootContentEvent.fire(this, this);
	}

	@Override
	protected void onBind() {
		super.onBind();
		this.registerHandler(getView().getAuthButton().addClickHandler(
				new ClickHandler() {

					/**
					 * Calling login() will display a popup to the user the
					 * first time it is called. Once the user has granted access
					 * to the application, subsequent calls to login() will not
					 * display the popup, and will immediately result in the
					 * callback being given the token to use.
					 */
					@Override
					public void onClick(ClickEvent event) {
						AuthRequest req = new AuthRequest(GOOGLE_AUTH_URL,
								GOOGLE_CLIENT_ID)
								.withScopes(BUZZ_READONLY_SCOPE);

						AUTH.login(req, new Callback<String, Throwable>() {
							@Override
							public void onSuccess(String token) {
								Window.alert("Got an OAuth token:\n" + token);
								OAuthPresenter.this.authToken = token;
							}

							@Override
							public void onFailure(Throwable caught) {
								Window.alert("Error:\n" + caught.getMessage());
							}
						});
					}

				}));
	}

	private static final String GOOGLE_AUTH_URL = "https://accounts.google.com/o/oauth2/auth";

	// This app's personal client ID assigned by the Google APIs Console
	// (http://code.google.com/apis/console).
	private static final String GOOGLE_CLIENT_ID = "957665152265-39reeckut5d28rkh2dh2lie443d9u58v.apps.googleusercontent.com";

	// The auth scope being requested. This scope will allow the application to
	// read Buzz activities, comments, etc., as if it was the user.
	private static final String BUZZ_READONLY_SCOPE = "https://www.googleapis.com/auth/buzz.readonly";

}
